package com.moore.controller;

import com.moore.dto.ResponseData;
import com.moore.service.PermissionService;
import com.moore.service.RolePermissionService;
import com.moore.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.moore.utils.Logger.toLog;

@RestController
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    RolePermissionService rolePermissionService;

    @GetMapping("/roles")
    public ResponseData getAllRoles(){
        toLog("获取所有角色");
        return roleService.getAllRoles();
    }

    @GetMapping("/permissions")
    public ResponseData getAllPermissions(){
        toLog("获取所有权限");
        return permissionService.getAllPermissions();
    }

    @GetMapping("/roles/{roleId}/permissions")
    public ResponseData getPermissionsByRoleId(@PathVariable int roleId) {
        toLog("正在获取角色ID为" + roleId + "的权限");
        return rolePermissionService.getPermissionsByRoleId(roleId);
    }

    @PostMapping("/roles/{roleId}/permissions")
    public ResponseData updateRolePermissions(@PathVariable int roleId, @RequestBody List<Integer> permissionIds){
        toLog("正在更新角色ID为" + roleId + "的权限");
        return rolePermissionService.updateRolePermissions(roleId, permissionIds);
    }
}
